package com.example.wk.zyfypt_218wk.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.wk.zyfypt_218wk.R;
import com.example.wk.zyfypt_218wk.bean.LoginBean;
import com.example.wk.zyfypt_218wk.bean.SessionIDBean;
import com.example.wk.zyfypt_218wk.iface.LoginListener;
import com.example.wk.zyfypt_218wk.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginListener{

    private EditText user,password;//用户名密码
    private Button login,register;//登录注册按钮
    private LoginModel loginModel;
    private SessionIDBean sessionIDBean;
    private Switch sw;
    private SharedPreferences sp;
    private String username="",pass="";
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();

    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            user.setText(name);
            password.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init(){
        user=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        login=(Button)findViewById(R.id.button_login);
        register=(Button)findViewById(R.id.button_regactivity);
        //注册监听器
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        sw=(Switch) findViewById(R.id.switch1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_login:
                loginModel=new LoginModel();
                username=user.getText().toString();
                pass=password.getText().toString();
                loginModel.getResultList(user.getText().toString(),password.getText().toString(),this);
                if(sw.isChecked()){
                    saveSP();
                }
                break;
            case R.id.button_regactivity:
                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent2);
                break;
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username", username);
        editor.putString("password", pass);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }

    @Override
    public void onResponse(LoginBean beanList) {
        Editable msg=user.getText();
        Toast.makeText(this, "欢迎"+msg+"登录", Toast.LENGTH_SHORT).show();
        //sessionIDBean=new SessionIDBean();
        sessionIDBean.getSessionIDBean=beanList.getSessionid();
        sessionIDBean.UserName=user.getText().toString();
        Intent intent3=new Intent(LoginActivity.this,Homepage.class);
        startActivity(intent3);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
